#!/usr/bin/perl
#rsfsd.pl

require "/sbin/webmin/syslog_w.pl";
require "./lvm-lib.pl";
my ($log_ev, $log_msg, $diskminor);
$diskminor = "lvm";

my $lvname=$ARGV[0];
my $lvsize=$ARGV[1];
my $newsize = $ARGV[2];
my $lvpath = $ARGV[3];
my $newblock = $ARGV[4];

my $rsfs_lock='/var/nas/lock/resizefsd';

my $err_msg; 
$out = `umount $lvpath`;
if($?){
	$err_msg = "Error:".$text{'err_lvbusy'}; #Device busy can not umount
	goto ABORT;
}

if($newsize < $lvsize){
	#Filesystem check...
	$out = `e2fsck -f $lvname`;
	$rc1 = $?>>8;
	if($rc1 && !($rc1 == 8)){
		$err_msg = "Error:".$text{'err_fsck'};
		$out = `mount -o usrquota -o grpquota $lvname $lvpath`;
		goto ABORT;
	}
	#Resize filesystem...
	$out = `/sbin/resize2fs -f $lvname $newblock`;
	$rc2 = $?>>8;
	if(!($rc2 == 0) && !($rc2 == 1)){
		$err_msg = "Error:".$text{'err_resizefs'};
		$out = `mount -o usrquota -o grpquota $lvname $lvpath`;
		goto ABORT;
	}
}

#Resize Logical Volume...
$rc = resize_lv($lvname, $lvsize, $newsize);
if($rc){
	$out = `mount -o usrquota -o grpquota $lvname $lvpath`;
	$err_msg = "Error:".$text{'err_resizelv'};
	goto ABORT;
}

if($newsize > $lvsize){
	#Filesystem check...
	$out = `e2fsck -f $lvname`;
        $rc3 = $?>>8;
        if($rc3 && !($rc3 == 8)){
                $err_msg = "Error:".$text{'err_fsck'};
                $out = `mount -o usrquota -o grpquota $lvname $lvpath`;
                goto ABORT;
        }
	#Resize filesystem...
	$out = `/sbin/resize2fs -f $lvname`;
        $rc4 = $?>>8;
        if(!($rc4 == 0) && !($rc4 == 1)){
                $err_msg = "Error:".$text{'err_resizefs'};
                $out = `mount -o usrquota -o grpquota $lvname $lvpath`;
                goto ABORT;
        }
}

$out = `mount -o usrquota -o grpquota $lvname $lvpath`;

#------------------------------------------

	#failed
	#$log_ev = 15007;
	#$log_msg= "Format $volpath error. Can not make filesystem";
	#nas_syslog($diskminor, 'naslog_info', $log_ev, $log_msg);

#$out = `mount -o usrquota -o grpquota $mddev $voldir 2>&1`;

#if ($?) {
	#log
#	$log_ev = 15008;
#	$log_msg= "Format $volpath error, cannot mount the volume.";
#	nas_syslog($diskminor, 'naslog_info', $log_ev, $log_msg);
#	goto ABORT;
#}

$log_ev = 15011;
$log_msg= "Resize filesystem $lvname ok";
nas_syslog($diskminor, 'naslog_info', $log_ev, $log_msg);


ABORT:
	if($err_msg){
		`echo $err_msg > /root/err`;
	}
	unlink("/var/nas/lock/resizefsd");
	exit 0;

